<?php

/**
 *      [Discuz!] (C)2001-2099 Comsenz Inc.
 *      This is NOT a freeware, use is subject to license terms
 *
 *      $Id: misc_category.php 20759 2011-03-03 02:06:45Z liulanbo $
 */

if(!defined('IN_DISCUZ')) {
	exit('Access Denied');
}

$gquery = DB::query("SELECT f.fid, f.fup, f.type, f.name, ff.moderators, ff.extra, f.domain, f.catforumcolumns AS forumcolumns, f.styleid, ff.description, ff.seotitle, ff.seodescription, ff.keywords FROM ".DB::table('forum_forum')." f LEFT JOIN ".DB::table('forum_forumfield')." ff ON ff.fid=f.fid WHERE f.fid='$gid'");

$sql = !empty($_G['member']['accessmasks']) ? "SELECT f.fid, f.fup, f.type, f.name, f.threads, f.posts, f.todayposts, f.domain, f.catforumcolumns AS forumcolumns,
				f.lastpost, f.inheritedmod, ff.description, ff.seotitle, ff.seodescription, ff.keywords, ff.moderators, ff.icon, ff.viewperm, ff.extra, ff.redirect, a.allowview
				FROM ".DB::table('forum_forum')." f
				LEFT JOIN ".DB::table('forum_forumfield')." ff ON ff.fid=f.fid
				LEFT JOIN ".DB::table('forum_access')." a ON a.uid='$_G[uid]' AND a.fid=f.fid
				WHERE f.fup='$gid' AND f.status='1' AND f.type='forum' ORDER BY f.displayorder"
			: "SELECT f.fid, f.fup, f.type, f.name, f.threads, f.posts, f.todayposts, f.lastpost, f.inheritedmod, f.domain,
				ff.description, ff.seotitle, ff.seodescription, ff.keywords, ff.moderators, ff.icon, ff.viewperm, ff.extra, ff.redirect
				FROM ".DB::table('forum_forum')." f
				LEFT JOIN ".DB::table('forum_forumfield')." ff USING(fid)
				WHERE f.fup='$gid' AND f.status='1' AND f.type='forum' ORDER BY f.displayorder";

$query = DB::query($sql);
if(!DB::num_rows($gquery) || !DB::num_rows($query)) {
	showmessage('forum_nonexistence', NULL);
}

while(($forum = DB::fetch($gquery)) || ($forum = DB::fetch($query))) {
	$forum['extra'] = unserialize($forum['extra']);
	if(!is_array($forum['extra'])) {
		$forum['extra'] = array();
	}
	if($forum['type'] != 'group') {
		$threads += $forum['threads'];
		$posts += $forum['posts'];
		$todayposts += $forum['todayposts'];
		if(forum($forum)) {
			$forum['orderid'] = $catlist[$forum['fup']]['forumscount'] ++;
			$forum['subforums'] = '';
			$forumlist[$forum['fid']] = $forum;
			$catlist[$forum['fup']]['forums'][] = $forum['fid'];
			$fids .= ','.$forum['fid'];
		}
	} else {
		$forum['collapseimg'] = 'collapsed_no.gif';
		$collapse['category_'.$forum['fid']] = '';

		if($forum['moderators']) {
			$forum['moderators'] = moddisplay($forum['moderators'], 'flat');
		}
		$catlist[$forum['fid']] = $forum;

		$navigation = '<em>&rsaquo;</em> '.$forum['name'];
		$navtitle_g = strip_tags($forum['name']);
	}
	if(is_array($forum['lastpost'])){
	/*
	 * Author: Mr.Cuong
	 * Create for 3 list new post in index_forum.php
	 * */
		// Cho nay de chen vao function bai moi
		// Lay danh sach post trong sub forum dua vao mang
		$query_l = DB::query("SELECT t.*, p.*
							  FROM " . DB::table('forum_thread') . " AS t
							  LEFT JOIN " . DB::table('forum_forum') . " AS c ON t.fid=c.fid
							  LEFT JOIN  " . DB::table('forum_post') . " AS p ON t.tid = p.tid
							  WHERE c.fup = " . $gid . " AND t.readperm <= " . $_G['group']['readaccess'] . " AND p.invisible >= '0' AND p.first = 1
							  ORDER BY lastpost DESC
						 	  LIMIT 0, 9");
		$intPost = 0;
		$_G['forum_colorarray'] = array('', '#EE1B2E', '#EE5023', '#996600', '#3C9D40', '#2897C5', '#2B65B7', '#8F2A90', '#EC1282');
		while($list = DB::fetch($query_l)){
			
			$post_p = $list['message'];
			
			if($list['attachment'] == 2 && preg_match_all("/\[attach\](\d+)\[\/attach\]/i", $post_p, $matchaids)) {
				$list['picatt'] = isAttachImage($matchaids[1]);
				unset($matchaids);
			}elseif(!$list['picatt'] && $list['attachment'] == 2){
				$list['picatt'] = getImgIsAtt($list['tid']);
			}elseif(!$list['picatt'] && preg_match_all("/\[img\](.*?)\[\/img\]/i", $post_p, $matchaids)) {
					$list['picurl'] = $matchaids[1][0];
					unset($matchaids);
			}elseif(!$list['picatt'] && !$list['picurl'] && preg_match_all("/\[img=.*?\](.*?)\[\/img\]/i", $post_p, $matchaids)){
				$list['picurl'] = $matchaids[1][0];
				unset($matchaids);
			}
									
			$post_p = preg_replace("/\[attach\](\d+)\[\/attach\]/i", '', $post_p);
			$post_p = preg_replace('/\[(img)\](.*?)\[\/(img)\]/si','',$post_p);
			$post_p = preg_replace('/\[i=s\](.*?)\[\/i\]/si','',$post_p);
			$post_p = preg_replace('/\[url=(.*?)\](.*?)\[\/url\]/si','',$post_p);
			$post_p = preg_replace('/\[(.*?)\]/si','',$post_p);
			$list['message'] = $post_p;
			//Lay ra thoi gian hien tai
			$todaytime = vn_mktime(dgmdate(TIMESTAMP,'dmY'));
			//Xu ly hien icon hien thi
			$list['moved'] = $list['heatlevel'] = $list['new'] = 0;
			$list['icontid'] = $list['forumstick'] || ! $list['moved'] && $list['isgroup'] != 1 ? $list['tid'] : $list['closed'];
			if($_G['forum']['status'] != 3 && ($list['closed'] || ($_G['forum']['autoclose'] && TIMESTAMP - $list[$closedby] > $_G['forum']['autoclose']))){
				if($list['isgroup'] == 1){
					$list['folder'] = 'common';
					$grouptids[] = $list['closed'];
				}else{
					$list['folder'] = 'lock';
				}
			}elseif($_G['forum']['status'] == 3 && $list['closed'] == 1){
				$list['folder'] = 'lock';
			}else{
				$list['folder'] = 'common';
				$list['weeknew'] = TIMESTAMP - 604800 <= $list['dbdateline'];
				if($list['replies'] > $list['views']){
					$list['views'] = $list['replies'];
				}
				if($_G['setting']['heatthread']['iconlevels']){
					foreach($_G['setting']['heatthread']['iconlevels'] as $k=>$i){
						if($list['heats'] > $i){
							$list['heatlevel'] = $k + 1;
							break;
						}
					}
				}
			}
			
			if($list['highlight']) {
				$string = sprintf('%02d', $list['highlight']);
				$stylestr = sprintf('%03b', $string[0]);

				$list['highlight'] = ' style="';
				$list['highlight'] .= $stylestr[0] ? 'font-weight: bold;' : '';
				$list['highlight'] .= $stylestr[1] ? 'font-style: italic;' : '';
				$list['highlight'] .= $stylestr[2] ? 'text-decoration: underline;' : '';
				$list['highlight'] .= $string[1] ? 'color: '.$_G['forum_colorarray'][$string[1]] : '';
				$list['highlight'] .= '"';
			} else {
				$list['highlight'] = '';
			}
			// xu ly thoi gian bai post
			$list['istoday'] = $list['dateline'] > $todaytime ? 1 : 0;
			$list['dbdateline'] = $list['dateline'];
			$list['dateline'] = dgmdate($list['dateline'],'d');
			$list['dblastpost'] = $list['lastpost'];
			$list['lastpost'] = dgmdate($list['lastpost'],'u');
			// tiep tuc xu ly icon
			if(in_array($list['displayorder'],array(1, 2, 3, 4))){
				$list['id'] = 'stickthread_' . $list['tid'];
			}else{
				$list['id'] = 'normalthread_' . $list['tid'];
				//Kiem tra lan xem gan day nhat
				// Kiem tra dien dan lan cuoi ghe tham qua bien cookie
				$forumlastvisit = 0;

				if(empty($_G['forum']['picstyle']) && isset($_G['cookie']['forum_lastvisit']) && strexists($_G['cookie']['forum_lastvisit'],'D_' . $list['fid'])){
					preg_match('/D\_' . $list['fid'] . '\_(\d+)/',$_G['cookie']['forum_lastvisit'],$a);
					$forumlastvisit = $a[1];
					unset($a);
				}
				dsetcookie('forum_lastvisit',preg_replace("/D\_" . $list['fid'] . "\_\d+/",'',$_G['cookie']['forum_lastvisit']) . 'D_' . $list['fid'] . '_' . TIMESTAMP,604800);

				if($list['folder'] == 'common' && $list['dblastpost'] >= $forumlastvisit || ! $forumlastvisit){
					$list['new'] = 1;
					$list['folder'] = 'new';
					$list['weeknew'] = TIMESTAMP - 604800 <= $list['dbdateline'];
				}
			}
			// ket thuc xu ly icon
			//Gan $list cua 1 subparent vao mot parent
			$listpost_wforum[$forum['fid']][] = $list;
			//return;
		}
	// End
	}
	
}
if($catlist) {
	foreach($catlist as $key => $var) {
		if($var['forumscount'] && $var['forumcolumns']) {
			$catlist[$key]['forumcolwidth'] = (floor(100 / $var['forumcolumns']) - 0.1).'%';
			$catlist[$key]['endrows'] = '';
			if($colspan = $var['forumscount'] % $var['forumcolumns']) {
				while(($var['forumcolumns'] - $colspan) > 0) {
					$catlist[$key]['endrows'] .= '<td>&nbsp;</td>';
					$colspan ++;
				}
				$catlist[$key]['endrows'] .= '</tr>';
			}
		}
	}
}
$query = DB::query("SELECT fid, fup, name, threads, posts, todayposts FROM ".DB::table('forum_forum')." WHERE status='1' AND fup IN ($fids) AND type='sub' ORDER BY displayorder");
while($forum = DB::fetch($query)) {

	if($_G['setting']['subforumsindex'] && $forumlist[$forum['fup']]['permission'] == 2) {
		$forumlist[$forum['fup']]['subforums'] .= '<a href="forum.php?mod=forumdisplay&fid='.$forum['fid'].'"><u>'.$forum['name'].'</u></a>&nbsp;&nbsp;';
	}
	$forumlist[$forum['fup']]['threads'] 	+= $forum['threads'];
	$forumlist[$forum['fup']]['posts'] 	+= $forum['posts'];
	$forumlist[$forum['fup']]['todayposts'] += $forum['todayposts'];

}

?>